class Solution {
public:
    vector<int> topKFrequent(vector<int>& nums, int k) {
        unordered_map<int, int> map; // 元素和次数
        for(auto& e : nums)
        {
            map[e]++;
        }
        priority_queue<pair<int, int>> pq; // 次数和元素
        for(auto it = map.begin(); it != map.end(); ++it)
        {
            pq.emplace(it->second, it->first);
        }
        vector<int> ret(k);
        while(k--)
        {
            auto [a, b] = pq.top();
            pq.pop();
            ret[k] = b;
        }
        return ret;
    }
};